package com.gongpingjialoan.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.DelearActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.bean.GoodDealerBean;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.Utils;
import com.google.gson.Gson;

/**
 * Created by Administrator on 2016/11/18.
 */
public class GoodDealersView extends FrameLayout {

    private LinearLayout mainLinearLayout;

    private NetDataJson mNetDataJson;

    public GoodDealersView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(getContext()).inflate(R.layout.good_dealers_view, this, true);
        mainLinearLayout = (LinearLayout) findViewById(R.id.dealers_main);
        getDealersData();
    }


    public void getDealersData() {
        setVisibility(View.GONE);
        if (mNetDataJson == null) {
            mNetDataJson = new NetDataJson(new NetDataJson.OnStringListener() {
                @Override
                public void onDataJsonError(String errorMessage) {
                    setVisibility(View.GONE);
                }

                @Override
                public void onDataJsonUpdate(String json) {
                    Gson gson = new Gson();
                    GoodDealerBean  goodDealerBean = gson.fromJson(json , GoodDealerBean.class);
                    if("success".equals(goodDealerBean.getStatus())){
                        int size = goodDealerBean.getData().getDealers().size();
                        if(size == 0){
                            setVisibility(View.GONE);
                        }else{
                            setVisibility(View.VISIBLE);
                            mainLinearLayout.removeAllViews();
                            for (int i = 0; i < size; i++) {
                                final GoodDealerBean.DataEntity.DealersEntity dealersEntit = goodDealerBean.getData().getDealers().get(i);
                                if(dealersEntit.getOn_sale_count() == 0)  continue;
                                View view = LayoutInflater.from(getContext()).inflate(R.layout.good_dealers_item, null);
                                TextView titleTextView = (TextView) view.findViewById(R.id.title);
                                TextView numTextView = (TextView) view.findViewById(R.id.nums);
//                                LinearLayout stars = (LinearLayout) view.findViewById(R.id.star);
                                titleTextView.setText(dealersEntit.getCompany_name());
                                String numString = "有" + dealersEntit.getOn_sale_count() + "辆在售";
                                String nums = dealersEntit.getOn_sale_count() + "";
                                numTextView.setText(Utils.changeColor(getContext(), numString, 1, 1 + nums.length(), R.color.discount_price_color));
//                                double rate =  dealersEntit.getRate();
//                                for (int j = 0; j < Math.floor(rate); j++) {
//                                    ImageView imageView = (ImageView) stars.getChildAt(j);
//                                    imageView.setImageResource(R.drawable.star);
//                                }
//                                if(rate > Math.floor(rate)){
//                                    ImageView imageView = (ImageView) stars.getChildAt((int) Math.floor(rate));
//                                    imageView.setImageResource(R.drawable.ban_star);
//                                }
                                view.setOnClickListener(new OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(getContext(), DelearActivity.class);
                                        intent.putExtra("domain", dealersEntit.getCompany_name());
                                        intent.putExtra("dealer_id", dealersEntit.getId() + "");
                                        intent.putExtra("city", dealersEntit.getCity());
                                        getContext().startActivity(intent);
                                    }
                                });
                                mainLinearLayout.addView(view);
                            }
                            if(mainLinearLayout.getChildCount() == 0){
                                setVisibility(View.GONE);
                            }
                        }
                    }
                }
            }, 1);
        }
        mNetDataJson.setUrl("/mobile/api/search-dealers/");
        mNetDataJson.addParam("city" , MainActivity.main.currentCity);
        mNetDataJson.request("get");
    }


    public void canalTask(){
        if(mNetDataJson!=null){
            mNetDataJson.cancelTask();
            mNetDataJson = null;
        }
    }
}
